#version 430 core
#include "DVR_Common.glsl"
//precision highp float;

in vec3 v2f_RayEntry;

out vec4 Color_;

uniform vec4 u_CameraPosInTex;

// Unit AABB
vec3 computeRayExit(const in vec3 vEntry, const in vec3 vDirection) 
{
  vec3 RayDir = modifyVecNE2Zero(vDirection);

  vec3 DirectionInverse = 1.0f / RayDir;
  vec3 tMin = -vEntry * DirectionInverse;
  vec3 tMax = (1.0f - vEntry) * DirectionInverse;
  vec3 t1 = min(tMin, tMax);
  vec3 t2 = max(tMin, tMax);
  float tNear = max(max(t1.x, t1.y), t1.z);
  float tFar = min(min(t2.x, t2.y), t2.z);

  return tFar * RayDir + vEntry;
}

void main()
{
    Color_ = vec4(0.0f);

	vec3 Direction = normalize(v2f_RayEntry - u_CameraPosInTex.xyz);
	vec3 RayExit = computeRayExit(v2f_RayEntry, Direction);

	int NumSteps = 0;
	vec3 StepSize = computeStepSize(v2f_RayEntry, RayExit, Direction, NumSteps);
	Color_ = accumulateColor(v2f_RayEntry, 0, NumSteps, StepSize);
}
